package com.java.user.controller;

import com.java.param.AddressListParam;
import com.java.param.AddressParam;
import com.java.param.AddressRemoveParam;
import com.java.pojo.Address;
import com.java.user.service.AddressService;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 地址查看接口
     * @param addressListParam
     * @param result
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody @Validated AddressListParam addressListParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数错误，无法查询！");
        }
        return addressService.list(addressListParam.getUserId());
    }

    /**
     * 添加地址数据
     * @param result
     * @return
     */
    @PostMapping("save")
    public R save(@RequestBody @Validated AddressParam addressParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数错误，插入失败！");
        }
        Address address = addressParam.getAdd();
        address.setUserId(addressParam.getUserId());

        return addressService.save(address);
    }

    /**
     * 删除地址信息
     * @param addressRemoveParam 接收类
     * @param result
     * @return
     */
    @PostMapping("remove")
    public R remove(@RequestBody @Validated AddressRemoveParam addressRemoveParam,BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数错误，删除失败！");
        }
        return addressService.remove(addressRemoveParam.getId());
    }
}
